<?php
include( "function.php" );
class CAdmin {
    static function showall($page_list)
    {
            global $pageContent, $page, $user;
            $info = $user->get_all_user_info($page_list);


            /*MAKE USER ADMIN CONTROL PANEL */
            $xtpl = new XTemplate('users_info.html',PAGE_PATH.DS.DS.$page.DS.DS.'tpl');
                           
            $all_users_content = "";

            /*GET TABLE HEADER*/
            $xtpl = new XTemplate('users_info.html',PAGE_PATH.DS.DS.$page.DS.DS.'tpl');
            $xtpl->parse( 'header' );
            $all_users_content = $all_users_content.$xtpl->text('header');

            for($i=0; $i< count($info); $i++){
               // var_dump($info[$i]);
               $all_users_content = $all_users_content.get_user_info_html($alt= $i%2+1, $info[$i]);

            }
            
            
            /*MAKE PAGES NAVIGATOR*/
            $page_count = $user->get_page_count();
            $page_nav_html = get_page_nav_html($page_list, $page_count);
            
            try{
                if (have_permission($groupid=3)){
                    $xtpl = new XTemplate('home.html',PAGE_PATH.DS.DS.$page.DS.DS.'tpl');
                    $xtpl->assign('USERS', $all_users_content);
                    
                    $links = '<div ><a class="button" style="float:left;"  href="'.ABS_URL.'?page=admin&act=adduser"'.'">Add new user</a></div>';
                    $xtpl->assign('ADD_USER_BTN', $links);
                    $xtpl->assign('PAGE_NAV', $page_nav_html);
                    $xtpl->parse( 'main' );
                    $pageContent= $xtpl->text('main');
                }
            }catch (Exception $e){
                $pageContent = '<div id="warning" style="display:block">'.$e->getMessage().'</div>';
            }

    }


    static function add_user(){
        global $pageContent, $page, $user;
        $msg="";
        $xtpl = new XTemplate('adduser.html',PAGE_PATH.DS.DS.$page.DS.DS.'tpl');
        if (have_permission($groupid=3)){ //HAVE ADMIN PERM
            if(isset($_POST['submit'])){
                $groupid = trim($_POST['group']);
                $username = trim($_POST['username']);
                $password = trim($_POST['password1']);
                $password2 = trim($_POST['password2']);
                $displayname=trim($_POST['displayname']);
                $adviser_info = isset($_POST['adviser_info'])?trim($_POST['adviser_info']):"";
                $diemhoctap = isset($_POST['diemhoctap'])?trim($_POST['diemhoctap']):"";
                $diemrenluyen = isset($_POST['diemrenluyen'])?trim($_POST['diemrenluyen']):"";
                $student_info = isset($_POST['student_info'])?trim($_POST['student_info']):"";

                
                try{
                    $user->add_new_user($username, $password, $password2, $displayname, $groupid, $adviser_info, $diemhoctap, $diemrenluyen, $student_info);
                    $xtpl->assign('MSG', $msg);
                    $xtpl->assign('TOOLBAR_LINKS', '<a href="'.ABS_URL.'?page=admin&act=users" class="link">Back to users manager</a><a href="" class="link">Add another</a>');
                    $xtpl->parse( 'success_msg' );
                    $pageContent= $xtpl->text('success_msg');
                    unset($_POST);
                    return;

                } catch (Exception $e) {
                    $msg  = '<div id="warning" style="display:block;">'.$e->getCode()." : ".$e->getMessage().'</div>';
                }
            }

            $xtpl->assign('MSG', $msg);
            $xtpl->assign('GROUPBOX', create_group_dropbox($selected=0));
            $xtpl->parse( 'main' );
            $pageContent= $xtpl->text('main');
        }else{
            $pageContent = '<div id="msg" style="display:block;">No infomation found!</div>';
        }
    }


    static function edit_user($userid){
        global $pageContent, $page, $user;
        $msg="";
        $info = $user->get_user_info($userid);        
        $xtpl = new XTemplate('edituser.html',PAGE_PATH.DS.DS.$page.DS.DS.'tpl');
        $uId = $_SESSION["id"];
        if ($uId==$userid||  have_permission($groupid=3)){
            $usergroups_id = trim($info['usergroups_id']);
            $username = trim($info['username']);
            $displayname=trim($info['displayname']);
            $delete_link = ABS_URL.'?page=admin&act=deleteuser&userid='.$userid;
            $xtpl->assign('DELETE_LINK', $delete_link);
            $xtpl->assign('ID', $userid);
            $xtpl->assign('USERNAME', $username);
            $xtpl->assign('DISPLAYNAME', $displayname);
            if (have_permission($groupid=3)){
                $xtpl->assign('HOCTAP', isset($info['diemhoctap'])?$info['diemhoctap']:"");
                $xtpl->assign('RENLUYEN', isset($info['diemrenluyen'])?$info['diemrenluyen']:"");
            }
            $xtpl->assign('SINHVIEN_INFO', isset($info['sinhvien_info'])?$info['sinhvien_info']:"");
            $xtpl->assign('GIAOVIEN_INFO', isset($info['giaovien_info'])?$info['giaovien_info']:"");
                        
            if (have_permission($groupid=3)){
                $xtpl->assign('GROUPBOX', create_group_dropbox($selected=$usergroups_id));
                $xtpl->parse( 'admin' );
                $ext_content = $xtpl->text('admin');
            }
            else if (have_permission ($groupid=2)){
                $xtpl->assign('GROUPBOX',"Sinh viên");
                $xtpl->parse( 'sinhvien' );
                $ext_content = $xtpl->text('sinhvien');
            }
            else if (have_permission ($groupid=1)){
                $xtpl->assign('GROUPBOX',"Giảng viên");
                $xtpl->parse( 'giaovien' );
                $ext_content = $xtpl->text('giaovien');
            }
            $xtpl->assign('EXT', $ext_content);
            if (isset($_POST['submit'])){
                try{
                    $username=trim($_POST['username']);
                    $displayname=trim($_POST['displayname']);
                    //$password=trim($_POST['password1']);
                    //$password2=trim($_POST['password2']);
                    $detai=isset($info['detai_id'])?$info['detai_id']:"";
                    $password=$_POST['password1']==""?$info[password]:$_POST['password1'];
                    $password2=$_POST['password2']==""?$info[password]:$_POST['password2'];

                    $usergroups_id=isset($_POST['group'])?trim($_POST['group']):"";
                    if (have_permission($groupid=2)) $usergroups_id = 2;
                    if (have_permission($groupid=1)) $usergroups_id = 1;
                    $adviser_info = isset($_POST['adviser_info'])?trim($_POST['adviser_info']):"";
                    $diemhoctap = isset($_POST['diemhoctap'])?trim($_POST['diemhoctap']):"";
                    $diemrenluyen = isset($_POST['diemrenluyen'])?trim($_POST['diemrenluyen']):"";
                    $student_info = isset($_POST['student_info'])?trim($_POST['student_info']):"";
                    //$detai = $info['detai_id'];
                    $user->edit_exist_user($groupid,$userid, $username, $displayname, $password, $password2, $usergroups_id, $adviser_info, $diemhoctap, $diemrenluyen, $student_info,$detai);
                    if (have_permission($groupid = 3))
                    $xtpl->assign('TOOLBAR_LINKS', '<a href="'.ABS_URL.'?page=admin&act=users" class="link">Back to users manager</a>');
                    $xtpl->parse( 'success_msg' );
                    $pageContent= $xtpl->text('success_msg');
                    unset($_POST);
                    return;

                } catch (Exception $e) {
                    $msg  = '<div id="warning" style="display:block;">'.$e->getCode()." : ".$e->getMessage().'</div>';
                }
            }
            
            $xtpl->assign('MSG', $msg);
            $xtpl->parse( 'main' );
            $pageContent= $xtpl->text('main');
        }else{
            $pageContent = '<div id="msg" style="display:block;">No permission!</div>';
        }
    }

    static function view_user($userid){
                global $pageContent, $page, $user;
                if ($_SESSION['username']){
                    $info = $user->get_user_info($userid);
                    $xtpl = new XTemplate('userinfo.html',PAGE_PATH.DS.DS.$page.DS.DS.'tpl');
                    $xtpl->assign('GIAOVIEN_INFO', isset($info['giaovien_info'])?$info['giaovien_info']:"");
                    $xtpl->parse( 'giaovien_info' );
                    $giaovien_info= $xtpl->text('giaovien_info');
                    
                    $xtpl->assign('HOCTAP', isset($info['diemhoctap'])?$info['diemhoctap']:"");
                    $xtpl->assign('RENLUYEN', isset($info['diemrenluyen'])?$info['diemrenluyen']:"");
                    $xtpl->assign('SINHVIEN_INFO', isset($info['sinhvien_info'])?$info['sinhvien_info']:"");
                    $xtpl->parse( 'sinhvien_info' );
                    $sinhvien_info= $xtpl->text('sinhvien_info');
                    $ext_info = "";
                    switch ($info["usergroups_id"]){
                        case ADMIN_GROUP_ID:
                            $ext_info = "";
                            break;
                        case STUDENT_GROUP_ID:
                            $ext_info = $sinhvien_info;
                            break;
                        case ADVISER_GROUP_ID:
                            $ext_info = $giaovien_info;
                            break;
                        default:
                            break;
                    }
                     $links = '<a href="?page=admin&act=edituser&userid='.$userid.'" class="link">Edit this user</a>
                              <a href="?page=admin&act=deleteuser&userid='.$userid.'" class="link"
                                  onclick="return confirm(\'Are you sure want to delete this user?\')">  <font color="red">Delete this user</font></a>';

                    $xtpl->assign('USERNAME', $info["username"]);
                    $xtpl->assign('DISPLAYNAME', $info["displayname"]);
                    $xtpl->assign('USERID', $info["id"]);
                    $xtpl->assign('GROUP', $user->get_group_name($info["usergroups_id"]));
                    $xtpl->assign('LINKS', $links);
                    $xtpl->assign('EXT_INFO', $ext_info );
                    $xtpl->parse( 'main' );

                    $pageContent= $xtpl->text('main');
                }else{
                    $pageContent = '<div id="msg" style="display:block;">No infomation found!</div>';
                }

    }

    static function delete_user($userid){
                global $pageContent, $page, $user;
                $xtpl = new XTemplate('deleteuser.html',PAGE_PATH.DS.DS.$page.DS.DS.'tpl');
                $links = '<a href="?page=admin&act=showall" class="link">Back to users manager</a>';
                $xtpl->assign('TOOLBAR_LINKS', $links);
                try{
                    if ($user->delete_user($userid)){
                        $xtpl->assign('MSG', '<div id="msg">One user have been deleted!</div>');
                        $xtpl->parse( 'main' );
                        $pageContent= $xtpl->text('main');
                    }
                }catch(Exception $e){
                    $xtpl->assign('MSG', '<div id="warning" style="display:block;">'.$e->getMessage().'</div>');
                    $xtpl->parse( 'main' );
                    $pageContent= $xtpl->text('main');
                }

    }
        


}
?>